import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    openId: '',
    userState: '',
    payList: [],  //  生成订单的商品
    payGoods: {}, //  立即购买生成订单的单个商品
    payOrder: {}, //  支付页需要的订单信息
    currentAddress: {}
  },
  mutations: {
    userState(state, userstate) {
      state.userState = userstate
    },
    setOpenId(state, data) {
      state.openId = data
    },
    pushList(state, list) {
      state.payList = list
    },
    payGoods(state, data) {
      state.payGoods = data
    },
    payOrder(state, data) {
      state.payOrder = data
    },
    switchAddress(state, data) {
      state.currentAddress = data
    }
  },
  actions: {
    userState({ commit }, userstate) {
      commit('userState', userstate)
    },
    setOpenId({ commit }, data) {
      commit('setOpenId', data)
    },
    pushList({ commit }, list) {
      commit('pushList', list)
    },
    payGoods({ commit }, data) {
      commit('payGoods', data)
    },
    payOrder({ commit }, data) {
      commit('payOrder', data)
    },
    switchAddress({ commit }, data) {
      commit('switchAddress', data)
    }
  },
  plugins: [createPersistedState({
    storage: window.sessionStorage
  })]
})
